const app = getApp(),url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile:'',
		code:'',
		// 倒计时参数
		timeStart:60, //倒计时初始值
    timeCur:null, //当前倒计时显示值
    ifTimeIn:false, //是否倒计时中
		success:false,

		blurts:false,
		clurts:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 获取手机号
  setMobile(e){
    let that = this
		// console.log(e.detail.value);
		that.setData({
			mobile : e.detail.value.replace(/\s+/g,"")//去空格
		});
  },
  setCode(e){
    let that = this
		that.setData({
      code : e.detail.value.replace(/\s+/g,""),
		});
  },
  // 倒计时
	setTime(){
		let timeCur = this.data.timeCur - 1;
		// console.log(timeCur);
		if(timeCur < 0){
			clearInterval(this.data.timer);
			this.setData({
				ifTimeIn:false
			});
			return false;
		}
		this.setData({
			timeCur : timeCur
		});
  },
  // 获取验证码
	getMobileVerify(){
		var that = this
		if(!that.data.mobile){
			wx.showModal({
				title: '友情提示',
				content: '请输入手机号',
				showCancel: false,
			});
			return false
    }
		if(!/^1\d{10}$/.test(that.data.mobile)){
			wx.showModal({
				title: '友情提示',
				content: '请输入正确的手机号',
				showCancel: false,
			});
			return false;
		}
		wx.showLoading({
		  title: "发送中",
		  mask: true
		});
		//enterprise/sendSmsMessage
		wx.request({
      url:url+'enterprise/sendSmsMessage',
      method: "GET",
      data:{
				mobile_no:that.data.mobile
			},
      success:function(res){
        console.log("code",res);
        var ret = res.data
        if(ret.code == 200){
					/* ----请求后台发送验证码成功---- */
					// 执行倒计时
					var str=ret.msg;//截取后4位
					var FScode = str.substring(str.length-6);
					console.log(FScode)
					that.setData({
						timeCur : that.data.timeStart,
						timer : setInterval(that.setTime,1000),
						ifTimeIn : true,
						success:true,
						FScode:FScode,
						// ifSendMobileVerify : true
					});
					/* ----请求后台发送验证码成功---- */
					// that.setData({listdata:ret.data.content})
					wx.hideLoading();
					wx.showToast({
            title: '发送成功',
            icon:'none'
					})
        }else{
          wx.showToast({
            title: '发送失败',
            icon:'none'
					})
					wx.hideLoading();
        }
      }
    })
  },
		
		
		
// 确定提交
bindDo(){
	var that = this
	that.setData({ success:false,})
	var openid = wx.getStorageSync('user_massage')
	var phone = that.data.mobile
	var FScode = that.data.FScode
	if(!that.data.code || FScode != that.data.code ){
		wx.showModal({
			title: '友情提示',
			content: '请输入正确验证码',
			showCancel: false,
		});
		that.setData({ success:true,})
		return false;
	}
	wx.request({
		url:url+'wx_commen/up_user_name_by_open_id',
		method: "GET",
		data:{
			open_id:openid.openId,
			phone:phone,
		},
		success:function(resh){
			console.log("确定提交",resh);
			if(resh.data.code == 'success'){
				wx.showToast({
					title: '成功',
					icon:'none',
				})
				wx.setStorageSync('phone', phone)
				setTimeout(function(){
					wx.navigateBack({
						delta: 1,
					})
				},1500)
			}else{
				wx.showToast({
					title: '失败',
					icon:'none',
				})
			}
		}
	})
},
// https://www.qugouzhigong.top/dgdx-Api/wx_commen/up_user_name_by_open_id?open_id=oqBnt4kH9qGh15fBqjV5E53vzZMw&phone=18741067595


	// 手机号验证
	phoneOnBlur(e){
		var that = this
		var blur = e.detail.value
		if(blur == ''){
			console.log('1')
			that.setData({blurts:true})
		}else{
			that.setData({blurts:false})
		}
	},
	// 验证码验证
	codeOnBlur(e){
		var that = this
		var clur = e.detail.value
		if(clur == ''){
			console.log('1')
			that.setData({clurts:true})
		}else{
			that.setData({clurts:false})
		}
	},
})